﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCWeb.Models;

namespace MVCWeb.Controllers
{
    public class CustomersController : Controller
    {
        //
        // GET: /Customers/

        public ActionResult Index(int custid)
        {
            //PLCustomer customer = CustomerClientServices.GetCustomerDetails(id);
            //ViewData["breadCrumbData"] = "Customer Details";

           // List<PLCustomer> list = CustomerClientServices.GetCustomerList();
            CustomerModel customer = CustomerClientServices.GetCustomerDetails(custid);
            ViewData["breadCrumbData"] = "Customer List";
            return View("Index", customer);
        }


        public ActionResult WebIndex(int custid)
        {
            //PLCustomer customer = CustomerClientServices.GetCustomerDetails(id);
            //ViewData["breadCrumbData"] = "Customer Details";

            // List<PLCustomer> list = CustomerClientServices.GetCustomerList();
            CustomerModel customer = CustomerClientServices.GetCustomerDetails(custid);
            ViewData["breadCrumbData"] = "Customer List";
            return View("WebIndex", customer);
        }
        //
        // GET: /Customers/Details/5

        public ActionResult Details(int id)
        {

            CustomerModel customer = CustomerClientServices.GetCustomerDetails(id);
            return View("Details", customer);
        }

        //
        // GET: /Customers/Create

        public ActionResult Create()
        {
          UrlHelper url = new UrlHelper(HttpContext.Request.RequestContext);
          CustomerModel customer = new CustomerModel();
          return View("Create", customer);
        } 

        //
        // POST: /Customers/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                CustomerModel customer = new CustomerModel();
                customer.customer_id = Convert.ToInt32(collection["customer_id"]);
                customer.email = collection["email"];
                customer.first_name = collection["first_name"];
                customer.last_name = collection["last_name"];
                customer.address = collection["address"];
                customer.city = collection["city"];
                customer.state = collection["state"];
                customer.zip = Convert.ToInt32(collection["zip"]);
                customer.permission = Convert.ToInt32(collection["permission"]);
                customer.blockuser = Convert.ToInt32(collection["blockuser"]);
                CustomerClientServices.CreateCustomer(customer);
                return View("Congrats");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Customers/Edit/5
 
        public ActionResult Edit(int id)
        {

            CustomerModel customer = CustomerClientServices.GetCustomerDetails(id);
            return View("Edit", customer);
        }

        //
        // POST: /Customers/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                CustomerModel customer = new CustomerModel();
                customer.customer_id = Convert.ToInt32(collection["customer_id"]);
                customer.email = collection["email"];
                customer.first_name = collection["first_name"];
                customer.last_name = collection["last_name"];
                customer.address = collection["address"];
                customer.city = collection["city"];
                customer.state = collection["state"];
                customer.zip = Convert.ToInt32(collection["zip"]);
                customer.permission = Convert.ToInt32(collection["permission"]);
                customer.blockuser = Convert.ToInt32(collection["blockuser"]);
                CustomerClientServices.UpdateResidence(id, customer);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult WebEdit(int id)
        {
            UrlHelper url = new UrlHelper(HttpContext.Request.RequestContext);
            ViewData["breadCrumbData"] = "<a href='" + url.Action("Index", "Customers") + "'>Customer List</a>";
            ViewData["breadCrumbData"] += " > Edit";

            CustomerModel customer = CustomerClientServices.GetCustomerDetails(id);
            return View("WebEdit", customer);
        }

        //
        // POST: /Customers/Edit/5

        [HttpPost]
        public ActionResult WebEdit(int id, FormCollection collection)
        {
            try
            {
                CustomerModel customer = new CustomerModel();
                customer.customer_id = Convert.ToInt32(collection["customer_id"]);
                customer.email = collection["email"];
                customer.first_name = collection["first_name"];
                customer.last_name = collection["last_name"];
                customer.address = collection["address"];
                customer.city = collection["city"];
                customer.state = collection["state"];
                customer.zip = Convert.ToInt32(collection["zip"]);
                customer.permission = Convert.ToInt32(collection["permission"]);
                customer.blockuser = Convert.ToInt32(collection["blockuser"]);
                CustomerClientServices.UpdateResidence(id, customer);
                return RedirectToAction("WebIndex");
            }
            catch
            {
                return View();
            }
        }
    }
}
